rm(list = ls())
library(foreign)
library(tidyverse)
library(gridExtra)
library(ggrepel)
library(readstata13)
library(viridis)
library(psych)
library(rgdal) # load shape files
library(broom) # transform shape files in data frames
library(here) # creates a path to the current directory
library(extrafont) # add new fonts

Sys.setenv(LANG = "en")

### Set Directory: this should work if you save the replication folder on your desktop, but please change as needed
setwd("~/Desktop/vw_negativity_replication/")

# Import the data sets with findings
dataraw <- read.dta13("thirdtry_collapsed_monthly.dta")
data <- read.dta13("findings_m.dta")

# Making a palette for the plots
palette_main <- c("blue", "black", "green4")

#########################################
## THINGS PRIOR TO THE ACTUAL ANALYSES ##
#########################################

## Descriptives of the outcome variables

# First I write a function for the plots
descriptives_outcome_plot <- function(outcome, title, color) {
  ggplot(dataraw) + 
    geom_density(aes(x = outcome), fill = color, color = color, alpha = 0.5) +
    xlab(" ") + 
    ggtitle(title) + 
    ylab(" ") + 
    theme_bw() +
    scale_x_continuous(limits = c(0, 4)) +
    theme(legend.position = "none",
          text = element_text(size =20),
          # remove the vertical grid lines
          panel.grid.major.y = element_blank(),
          strip.background = element_rect(fill = "white"))
}

# Then I make the plots for the negative emotions
fear_descriptive <- descriptives_outcome_plot(dataraw$fearnorm_stnd, "Fear", "black")
anger_descriptive <- descriptives_outcome_plot(dataraw$angernorm_stnd, "Anger", "red")
disgust_descriptive <- descriptives_outcome_plot(dataraw$disgustnorm_stnd, "Disgust", "green4")
sadness_descriptive <- descriptives_outcome_plot(dataraw$sadnessnorm_stnd, "Sadness", "blue")

joy_descriptive <- descriptives_outcome_plot(dataraw$joynorm_stnd, "Joy", "yellow3")
enthusiasm_descriptive <- descriptives_outcome_plot(dataraw$enthusiasmnorm_stnd, "Enthusiasm", "orange")
pride_descriptive <- descriptives_outcome_plot(dataraw$pridenorm_stnd, "Pride", "seagreen")
hope_descriptive <- descriptives_outcome_plot(dataraw$hopenorm_stnd, "Hope", "yellow3")

# Then I put all of them together
emotions_descriptives <- grid.arrange(
  fear_descriptive,
  anger_descriptive,
  disgust_descriptive,
  sadness_descriptive,
  joy_descriptive,
  enthusiasm_descriptive,
  pride_descriptive,
  hope_descriptive,
  nrow = 2,
  ncol = 4)
emotions_descriptives

ggsave("Drafts/outcomes_descriptives.png", plot = emotions_descriptives, width = 30, height = 20, units = "cm") 

## Plotting each emotion by party
# First I selecte only the main parties
datarawselected <- dataraw %>%
  filter(party_code %in% c(1, 2, 3, 4, 7, 8, 11))

# (Checking if this seems to have come out right)
table(datarawselected$party_code)

# Then I write a function to plot each emotion
plot_emotion <- function (emotion, ylab, title){
  # Describe the mean level of each emotion per party
  descriptives_this_emotion <- describeBy(emotion, datarawselected$party_code, mat = TRUE, digits = 10)
  
  # Name the grouping variable "Party"
  names(descriptives_this_emotion) [names(descriptives_this_emotion) == 'group1'] = 'Party'
  
  # Fix the party names
  levels(descriptives_this_emotion$Party)[levels(descriptives_this_emotion$Party) == '1'] = 'AfD'
  levels(descriptives_this_emotion$Party)[levels(descriptives_this_emotion$Party) == '2'] = 'CDU'
  levels(descriptives_this_emotion$Party)[levels(descriptives_this_emotion$Party) == '3'] = 'CSU'
  levels(descriptives_this_emotion$Party)[levels(descriptives_this_emotion$Party) == '4'] = 'FDP'
  levels(descriptives_this_emotion$Party)[levels(descriptives_this_emotion$Party) == '7'] = 'Greens'
  levels(descriptives_this_emotion$Party)[levels(descriptives_this_emotion$Party) == '8'] = 'The Left'
  levels(descriptives_this_emotion$Party)[levels(descriptives_this_emotion$Party) == '11'] = 'SPD'
  
  # Calculate standard error
  descriptives_this_emotion$se = descriptives_this_emotion$sd/ (sqrt (descriptives_this_emotion$n))
  
  # Calculate confidence intervals
  limits = aes (ymax = mean + (1.96 * se), ymin = mean - (1.96 * se))

  # Make the plot
  dodge = position_dodge(width = 0.9)
  
  descriptives_this_emotion %>%
    mutate(afd = ifelse((Party == 1), 1, 0)) %>%
    ggplot (aes(x = Party, y = mean, fill = as.factor(afd))) +
    geom_bar (stat = 'identity', alpha = 0.7) +
    geom_errorbar (limits, position = dodge, width = 0.0) +
    ylab (ylab) +
    xlab("Party of the MP") +
    scale_fill_manual(values = c("gray50", "red"))  +
    theme_bw() +
    theme(text = element_text(size = 20)) +
    guides(fill = FALSE) + 
    scale_x_discrete(labels = c("AfD", "SPD", "CDU", "CSU", "FDP", "Green", "Left")) +
    ggtitle(title)
}

# Then I make one plot for each of the dependent variables
anger_plot_descriptive <- plot_emotion(datarawselected$angernorm, "Level of anger in the speeches", "Anger")
fear_plot_descriptive <- plot_emotion(datarawselected$fearnorm, "Level of fear in the speeches", "Fear")
disgust_plot_descriptive <- plot_emotion(datarawselected$disgustnorm, "Level of disgust in the speeches", "Disgust")
sadness_plot_descriptive <- plot_emotion(datarawselected$sadnessnorm, "Level of sadness in the speeches", "Sadness")
joy_plot_descriptive <- plot_emotion(datarawselected$joynorm, "Level of joy in the speeches", "Joy")
enthusiasm_plot_descriptive <- plot_emotion(datarawselected$enthusiasmnorm, "Level of enthusiasm in the speeches", "Enthusiasm")
pride_plot_descriptive <- plot_emotion(datarawselected$pridenorm, "Level of pride in the speeches", "Pride")
hope_plot_descriptive <- plot_emotion(datarawselected$hopenorm, "Level of hope in the speeches", "Hope")

# Finally I put all plots together
outcomes_by_party_plot <- grid.arrange(
  anger_plot_descriptive,
  fear_plot_descriptive,
  disgust_plot_descriptive,
  sadness_plot_descriptive,
  joy_plot_descriptive,
  enthusiasm_plot_descriptive,
  pride_plot_descriptive,
  hope_plot_descriptive,
  nrow = 2,
  ncol = 4)
outcomes_by_party_plot

ggsave("Drafts/outcomes_by_party.png", plot = outcomes_by_party_plot, width = 48.5, height = 30, units = "cm") 

###################################################################
########################## MAIN ANALYSES ##########################
###################################################################

# Draw the plot with the main analyses
main_plot <- data %>%
  filter(sentiments == 1) %>%
  filter(Sample == "Without AfD") %>%
  filter(Months == 9999) %>%
  filter(Period == "Monthly") %>%
  filter(Specification == "Year fixed effects") %>%
  filter(Model == "Effect of AfD entry") %>%
  filter(Outcome != "Emotion score") %>%
  filter(Outcome != "Emotions (PCA)") %>%
  mutate(Outcome = fct_reorder(Outcome, coef), desc = T) %>%
  mutate(Outcome = fct_reorder(Outcome, pca), desc = F) %>%  
  ggplot(aes(Outcome, coef, color = factor(pca))) + 
  geom_hline(aes(yintercept = 0), linetype = 2) + 
  coord_flip(ylim = c(-2, 2)) + 
  geom_linerange(aes(ymin = ci_lower_95, ymax = ci_upper_95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  theme_minimal(base_size = 16) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = NA, color = "black")) +
  scale_color_manual(values = palette_main) +
  ylab(" ") + 
  xlab("") +
  facet_grid(outcome_type ~., scale = 'free') 
main_plot

ggsave("Drafts/main_plot.png", plot = main_plot, width = 20, height = 20, units = "cm") 

# Main analyses again, without excluding speeches with less than 100 words
main_plot_100 <- data %>%
  filter(sentiments == 1) %>%
  filter(Sample == "Short speeches removed") %>%
  filter(Months == 9999) %>%
  filter(Period == "Monthly") %>%
  filter(Specification == "Year fixed effects") %>%
  filter(Model == "Effect of AfD entry") %>%
  mutate(Outcome = fct_reorder(Outcome, coef), desc = T) %>%
  mutate(Outcome = fct_reorder(Outcome, pca), desc = F) %>%  
  ggplot(aes(Outcome, coef, color = factor(pca))) + 
  geom_hline(aes(yintercept = 0), linetype = 2) + 
  coord_flip(ylim = c(-2, 2)) + 
  geom_linerange(aes(ymin = ci_lower_95, ymax = ci_upper_95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  theme_minimal(base_size = 16) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = NA, color = "black")) +
  scale_color_manual(values = palette_main) +
  ylab(" ") + 
  xlab("") +
  facet_grid(outcome_type ~., scale = 'free') 
main_plot_100

ggsave("Drafts/main_plot_100.png", plot = main_plot_100, width = 20, height = 20, units = "cm") 

# Main analyses again, without excluding small parties
main_plot_allparties <- data %>%
  filter(sentiments == 1) %>%
  filter(Sample == "All parties (including small ones)") %>%
  filter(Months == 9999) %>%
  filter(Period == "Monthly") %>%
  filter(Specification == "Year fixed effects") %>%
  filter(Model == "Effect of AfD entry") %>%
  mutate(Outcome = fct_reorder(Outcome, coef), desc = T) %>%
  mutate(Outcome = fct_reorder(Outcome, pca), desc = F) %>%  
  ggplot(aes(Outcome, coef, color = factor(pca))) + 
  geom_hline(aes(yintercept = 0), linetype = 2) + 
  coord_flip(ylim = c(-2, 2)) + 
  geom_linerange(aes(ymin = ci_lower_95, ymax = ci_upper_95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  theme_minimal(base_size = 16) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = NA, color = "black")) +
  scale_color_manual(values = palette_main) +
  ylab(" ") + 
  xlab("") +
  facet_grid(outcome_type ~., scale = 'free') 
main_plot_allparties

ggsave("Drafts/main_plot_allparties.png", plot = main_plot_allparties, width = 20, height = 20, units = "cm") 

## Effect split by topic
bytopic_plot <- data %>%
  filter(Sample %in% c("Issues rad right MPs discuss the most", "Issues rad right MPs discuss the least")) %>%
  mutate(Outcome = fct_reorder(Outcome, coef), desc = T) %>%
  mutate(Outcome = fct_reorder(Outcome, pca), desc = F) %>%  
  ggplot(aes(Outcome, coef, color = Sample)) + 
  geom_hline(aes(yintercept = 0), linetype = 2, color = "black") + 
  geom_linerange(aes(ymin = ci_lower_95, ymax = ci_upper_95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  theme_bw(base_size = 20) +
  theme(legend.position = c(0.75, 0.2)) +
  scale_color_manual(values = c("gray40", "green4")) +
  ylab(" ") + 
  xlab("") +
  facet_grid(.  ~ outcome_type, scale = "free_x") 
bytopic_plot

ggsave("Drafts/bytopic_plot.png", plot = bytopic_plot, width = 50, height = 20, units = "cm") 

## Effect depending on government or opposition states by topic
gov_plot <- data %>%
  filter(Sample %in% c("MPs from previous gov parties", "MPs from subsequent gov parties", "MPs from re-elected gov parties")) %>%
  mutate(Outcome = fct_reorder(Outcome, coef), desc = T) %>%
  mutate(Outcome = fct_reorder(Outcome, pca), desc = F) %>%  
  ggplot(aes(Outcome, coef)) + 
  geom_hline(aes(yintercept = 0), linetype = 2, color = "black") + 
  geom_linerange(aes(ymin = ci_lower_95, ymax = ci_upper_95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  theme_bw(base_size = 14) +
  scale_color_manual(values = c("gray40", "green4")) +
  ylab(" ") + 
  xlab("") +
  facet_grid(Sample  ~ outcome_type, scale = "free_x") 
gov_plot

ggsave("Drafts/gov_plot.png", plot = gov_plot, width = 32, height = 22, units = "cm") 

opp_plot <- data %>%
  filter(Sample %in% c("MPs from previous opposition parties", "MPs from subsequent opposition parties", "MPs from re-elected opposition parties")) %>%
  mutate(Outcome = fct_reorder(Outcome, coef), desc = T) %>%
  mutate(Outcome = fct_reorder(Outcome, pca), desc = F) %>%  
  ggplot(aes(Outcome, coef)) + 
  geom_hline(aes(yintercept = 0), linetype = 2, color = "black") + 
  geom_linerange(aes(ymin = ci_lower_95, ymax = ci_upper_95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  theme_bw(base_size = 14) +
  scale_color_manual(values = c("gray40", "green4")) +
  ylab(" ") + 
  xlab("") +
  facet_grid(Sample  ~ outcome_type, scale = "free_x") 
opp_plot

ggsave("Drafts/opp_plot.png", plot = opp_plot, width = 33, height = 25, units = "cm") 

##################################################################################
## CHECKING IF THIS IS ENOUGH TO COUNTERBALANCE THE EFFECT OF THE RADICAL RIGHT ##
##################################################################################

datarawselected <- dataraw %>%
  mutate(Months = month - 58) %>%
  filter (Months <= 12) %>%
  filter (Months >= -12)

datarawselected$posnegdiff <- datarawselected$pos_sentiments_pca - datarawselected$neg_sentiments_pca

# Keeping only the main parties

evolution_plot <- datarawselected %>%
  # The variable months starts at 0, so I uncenter it to have 1 be the first month after
  mutate(Months = month - 58) %>%
 filter(party_code %in% c(1, 2, 3, 4, 7, 8, 11)) %>%
  ggplot(aes(x = Months, y = posnegdiff)) + 
#  ylim(-1, 1) +
  coord_cartesian(ylim = c(-0.1, 0.4)) +
  scale_x_continuous(breaks = c(-12, -9, -6, -3, 0, 3, 6, 9, 12)) +
  stat_summary_bin(fun.y = 'mean', bins = 24,
                   color = 'black', size = 0.75, geom = 'point', alpha = 0.5) +
  stat_smooth(method = "loess", formula = y ~ x, aes(colour = as.factor(after_state_elec)), size = 0.5, span = 3) +
  geom_vline(xintercept = 0.75, linetype = "dashed", color = "red") +
  geom_hline(yintercept = 0, linetype = "dashed", color = "black") +
  xlab("Months since state election") +
  ylab("Difference between the use of positive and negative emotions (SD's)") + 
  scale_colour_manual(name = " ", values = c("blue", "blue")) +
  guides(shape = FALSE) +
  theme(legend.position = "none") +
  theme(panel.background = element_rect(fill = "white", colour = "grey50")) 
evolution_plot

ggsave("Drafts/evolution.png", plot = evolution_plot, width = 20, height = 15, units = "cm") 

#####################################
## HETEROGENEOUS TREATMENT EFFECTS ##
#####################################

## Effects conditional on frequency of AfD speeches

hte_speechfreq <- data %>%
  filter(Sample == "Rad right speech frequency above median" | Sample == "Rad right speech frequency below median") %>%
  mutate(Outcome = fct_reorder(Outcome, coef), desc = T) %>%
  mutate(Outcome = fct_reorder(Outcome, pca), desc = F) %>%  
  mutate(ci_lower_90 = coef - 1.645 * stderr) %>%
  mutate(ci_upper_90 = coef + 1.645 * stderr) %>%
  ggplot(aes(Outcome, coef, color = factor(pca))) + 
  geom_hline(aes(yintercept = 0), linetype = 2, color = "red") + 
  geom_linerange(aes(ymin = ci_lower_95, ymax = ci_upper_95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  theme_minimal(base_size = 14) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = NA, color = "black")) +
  scale_color_manual(values = c("gray", "black")) +
  ylab(" ") + 
  xlab("") +
  facet_grid(Sample  ~ outcome_type, scale = "free_x") 
hte_speechfreq

ggsave("Drafts/hte_speechfreq.png", plot = hte_speechfreq, width = 33, height = 18, units = "cm") 

## Looking at effects split by left and right, excluding Die Linke

hte_leftright_nodielinke <- data %>%
  filter(Sample == "Left-wing MPs (excluding The Left)" | Sample == "Right-wing MPs (excluding AfD)") %>%
  filter(Months == 9999) %>%
  filter(Model == "Effect of AfD entry") %>%
  filter(Specification == "Year fixed effects") %>%
  mutate(Outcome = fct_reorder(Outcome, coef), desc = T) %>%
  mutate(Outcome = fct_reorder(Outcome, pca), desc = F) %>%  
  ggplot(aes(Outcome, coef, color = factor(pca))) + 
  geom_hline(aes(yintercept = 0), linetype = 2, color = "red") + 
  geom_linerange(aes(ymin = ci_lower_95, ymax = ci_upper_95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  theme_minimal(base_size = 14) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = NA, color = "black")) +
  scale_color_manual(values = c("gray", "black")) +
  ylab(" ") + 
  xlab("") +
  facet_grid(Sample  ~ outcome_type, scale = "free_x") 
hte_leftright_nodielinke

ggsave("Drafts/hte_leftright_nodielinke.png", plot = hte_leftright_nodielinke, width = 33, height = 15, units = "cm") 

## Looking at effects split by left and right, including Die Linke

sentiments_leftright <- data %>%
  filter(Sample == "Left-wing MPs" | Sample == "Right-wing MPs (excluding AfD)") %>%
  filter(Months == 9999) %>%
  filter(Model == "Effect of AfD entry") %>%
  filter(Specification == "Year fixed effects") %>%
  mutate(Outcome = fct_reorder(Outcome, coef), desc = T) %>%
  mutate(Outcome = fct_reorder(Outcome, pca), desc = F) %>%  
  ggplot(aes(Outcome, coef, color = factor(pca))) + 
  geom_hline(aes(yintercept = 0), linetype = 2, color = "red") + 
  geom_linerange(aes(ymin = ci_lower_95, ymax = ci_upper_95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  theme_minimal(base_size = 14) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = NA, color = "black")) +
  scale_color_manual(values = c("gray", "black")) +
  ylab(" ") + 
  xlab("") +
  facet_grid(Sample  ~ outcome_type, scale = "free_x") 
sentiments_leftright

ggsave("Drafts/hte_leftright.png", plot = sentiments_leftright, width = 33, height = 15, units = "cm") 

## Looking at effects split by party
# Just Die Linke vs. others
dielinke_vs_others <- data %>%
  filter(Sample %in% c("MPs from Die Linke", 
                       "MPs from mainstream parties")) %>%
  filter(Months == 9999) %>%
  filter(Model == "Effect of AfD entry") %>%
  filter(Specification == "Year fixed effects") %>%
  mutate(Outcome = fct_reorder(Outcome, coef), desc = T) %>%
  mutate(Outcome = fct_reorder(Outcome, pca), desc = F) %>%  
  ggplot(aes(Outcome, coef, color = factor(pca))) + 
  geom_hline(aes(yintercept = 0), linetype = 2, color = "red") + 
  facet_grid(Sample  ~ outcome_type, scale = "free_x") +
  geom_linerange(aes(ymin = ci_lower_95, ymax = ci_upper_95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  theme_minimal(base_size = 14) +
  scale_color_manual(values = c("gray", "black")) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = NA, color = "black")) +
  ylab(" ") + 
  xlab("") 
dielinke_vs_others

ggsave("Drafts/dielinke_vs_others.png", plot = dielinke_vs_others, width = 36, height = 15, units = "cm") 

# Same thing without including the Greens
dielinke_vs_others_nogreen <- data %>%
  filter(Sample %in% c("MPs from Die Linke", 
                       "MPs from mainstream parties (excluding Greens)")) %>%
  filter(Months == 9999) %>%
  filter(Model == "Effect of AfD entry") %>%
  filter(Specification == "Year fixed effects") %>%
  mutate(Outcome = fct_reorder(Outcome, coef), desc = T) %>%
  mutate(Outcome = fct_reorder(Outcome, pca), desc = F) %>%  
  ggplot(aes(Outcome, coef, color = factor(pca))) + 
  geom_hline(aes(yintercept = 0), linetype = 2, color = "red") + 
  facet_grid(Sample  ~ outcome_type, scale = "free_x") +
  geom_linerange(aes(ymin = ci_lower_95, ymax = ci_upper_95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  theme_minimal(base_size = 14) +
  scale_color_manual(values = c("gray", "black")) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = NA, color = "black")) +
  ylab(" ") + 
  xlab("") 
dielinke_vs_others_nogreen

ggsave("Drafts/dielinke_vs_others_nogreen.png", plot = dielinke_vs_others_nogreen, width = 36, height = 20, units = "cm") 

# Party by party
sentiments_parties <- data %>%
  filter(sentiments == 1) %>%
  filter(Sample %in% c("MPs from CDU + CSU", "MPs from Die Linke", "MPs from FDP", "MPs from Greens", "MPs from SPD")) %>%
  filter(Months == 9999) %>%
  filter(Model == "Effect of AfD entry") %>%
  filter(Specification == "Year fixed effects") %>%
  mutate(Outcome = fct_reorder(Outcome, coef), desc = T) %>%
  mutate(Outcome = fct_reorder(Outcome, pca), desc = F) %>%  
  ggplot(aes(Outcome, coef, color = factor(pca))) + 
  geom_hline(aes(yintercept = 0), linetype = 2) + 
  coord_flip( ) + 
  facet_grid(outcome_type ~ Sample, scales = 'free_y') + 
  geom_linerange(aes(ymin = ci_lower_95, ymax = ci_upper_95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  theme_minimal(base_size = 14) +
  scale_color_manual(values = palette_main) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = NA, color = "black")) +
  ylab(" ") + 
  xlab("") 
sentiments_parties

ggsave("Drafts/hte_parties.png", plot = sentiments_parties, width = 30, height = 20, units = "cm") 


######################## 
## ROBUSTNESS CHECKS ##
######################## 

# Main analyses using only MPs that were in state parliament in the previous legislature
incumbents_plot <- data %>%
  filter(Sample == "Incumbents") %>%
  filter(Months == 9999) %>%
  filter(Specification == "Year fixed effects") %>%
  filter(Model == "Effect of AfD entry") %>%
  mutate(Outcome = fct_reorder(Outcome, coef), desc = T) %>%
  mutate(Outcome = fct_reorder(Outcome, pca), desc = F) %>%  
  ggplot(aes(Outcome, coef, color = factor(pca))) + 
  geom_hline(aes(yintercept = 0), linetype = 2) + 
  coord_flip(ylim = c(-2, 2)) + 
  geom_linerange(aes(ymin = ci_lower_95, ymax = ci_upper_95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  theme_minimal(base_size = 16) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = NA, color = "black")) +
  scale_color_manual(values = palette_main) +
  ylab(" ") + 
  xlab("") +
  facet_grid(outcome_type ~., scale = 'free') 
incumbents_plot

ggsave("Drafts/incumbents_plot.png", plot = incumbents_plot, width = 20, height = 20, units = "cm") 

## Main analyses without collapsing by month-to-election
bydate_plot <- data %>%
  filter(sentiments == 1) %>%
  filter(Months == 9999) %>%
  filter(Period == "By date") %>%
  filter(Specification == "Month fixed effects") %>%
  filter(Model == "Effect of AfD entry") %>%
  mutate(Outcome = fct_reorder(Outcome, coef), desc = T) %>%
  mutate(Outcome = fct_reorder(Outcome, pca), desc = F) %>%  
  ggplot(aes(Outcome, coef, color = factor(pca))) + 
  geom_hline(aes(yintercept = 0), linetype = 2) + 
  coord_flip(ylim = c(-2, 2)) + 
  geom_linerange(aes(ymin = ci_lower_95, ymax = ci_upper_95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  theme_minimal(base_size = 16) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = NA, color = "black")) +
  scale_color_manual(values = palette_main) +
  ylab(" ") + 
  xlab("") +
  facet_grid(outcome_type ~ ., scale = 'free') 
bydate_plot

ggsave("Drafts/bydate_plot.png", plot = bydate_plot, width = 20, height = 20, units = "cm") 

## Main analyses with state fixed effects
statefe_plot <- data %>%
  filter(Specification == "State fixed effects") %>%
  mutate(Outcome = fct_reorder(Outcome, coef), desc = T) %>%
  mutate(Outcome = fct_reorder(Outcome, pca), desc = F) %>%  
  ggplot(aes(Outcome, coef, color = factor(pca))) + 
  geom_hline(aes(yintercept = 0), linetype = 2) + 
  coord_flip(ylim = c(-2, 2)) + 
  geom_linerange(aes(ymin = ci_lower_95, ymax = ci_upper_95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  theme_minimal(base_size = 16) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = NA, color = "black")) +
  scale_color_manual(values = palette_main) +
  ylab(" ") + 
  xlab("") +
  facet_grid(outcome_type ~., scale = 'free') 
statefe_plot

ggsave("Drafts/statefe_plot.png", plot = statefe_plot, width = 20, height = 20, units = "cm") 

## Main analyses adding calendar year FE
yearfe_plot <- data %>%
  filter(Months == 9999) %>%
  filter(Period == "Monthly") %>%
  filter(Specification == "Month and year fixed effects") %>%
  filter(Model == "Effect of AfD entry") %>%
  mutate(Outcome = fct_reorder(Outcome, coef), desc = T) %>%
  mutate(Outcome = fct_reorder(Outcome, pca), desc = F) %>%  
  ggplot(aes(Outcome, coef, color = factor(pca))) + 
  geom_hline(aes(yintercept = 0), linetype = 2) + 
  coord_flip(ylim = c(-2, 2.2)) + 
  geom_linerange(aes(ymin = ci_lower_95, ymax = ci_upper_95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  theme_minimal(base_size = 16) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = NA, color = "black")) +
  scale_color_manual(values = palette_main) +
  ylab(" ") + 
  xlab("") +
  facet_grid(outcome_type ~., scale = 'free') 
yearfe_plot

ggsave("Drafts/yearfe_plot.png", plot = yearfe_plot, width = 20, height = 20, units = "cm") 

## Main analyses with logged outcomes
log_plot <- data %>%
  filter(Specification == "Logged outcomes") %>%
  mutate(Outcome = fct_reorder(Outcome, coef), desc = T) %>%
  mutate(Outcome = fct_reorder(Outcome, pca), desc = F) %>%  
  ggplot(aes(Outcome, coef, color = factor(pca))) + 
  geom_hline(aes(yintercept = 0), linetype = 2) + 
  coord_flip(ylim = c(-2, 2)) + 
  geom_linerange(aes(ymin = ci_lower_95, ymax = ci_upper_95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  theme_minimal(base_size = 16) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = NA, color = "black")) +
  scale_color_manual(values = palette_main) +
  ylab(" ") + 
  xlab("") +
  facet_grid(outcome_type ~., scale = 'free') 
log_plot

ggsave("Drafts/log_plot.png", plot = log_plot, width = 20, height = 20, units = "cm") 

## Jackknives (analyses after removing one state at a time)
jk_plot <- data %>%
  filter(Model == "Jackknives") %>%
  mutate(Outcome = fct_reorder(Outcome, pca), desc = F) %>%  
  ggplot(aes(Outcome, coef, color = factor(pca))) + 
  geom_hline(aes(yintercept = 0), linetype = 2) + 
  coord_flip() + 
  geom_linerange(aes(ymin = ci_lower_95, ymax = ci_upper_95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  theme_minimal(base_size = 6) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = NA, color = "black")) +
  scale_color_manual(values = palette_main) +
  ylab("") + 
  xlab("") +
  facet_grid(outcome_type ~ State, scale = 'free_y') 
jk_plot

ggsave("Drafts/jk.png", plot = jk_plot, width = 50, height = 20, units = "cm")

## Main analyses with calendar month fixed effects
threefe <- data %>%
  filter(Specification == "Month and calendar month fixed effects") %>%
  mutate(Outcome = fct_reorder(Outcome, coef), desc = T) %>%
  mutate(Outcome = fct_reorder(Outcome, pca), desc = F) %>%  
  ggplot(aes(Outcome, coef, color = factor(pca))) + 
  geom_hline(aes(yintercept = 0), linetype = 2) + 
  coord_flip(ylim = c(-3, 3)) + 
  geom_linerange(aes(ymin = ci_lower_95, ymax = ci_upper_95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  theme_minimal(base_size = 16) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = NA, color = "black")) +
  scale_color_manual(values = palette_main) +
  ylab(" ") + 
  xlab("") +
  facet_grid(outcome_type ~., scale = 'free') 
threefe

ggsave("Drafts/threefe.png", plot = threefe, width = 20, height = 20, units = "cm") 

## Placebo using Hessen 2013 election
hessen2013 <- data %>%
  filter(Sample == "Placebo Hessen 2013") %>%
  mutate(Outcome = fct_reorder(Outcome, coef), desc = T) %>%
  mutate(Outcome = fct_reorder(Outcome, pca), desc = F) %>%  
  ggplot(aes(Outcome, coef, color = factor(pca))) + 
  geom_hline(aes(yintercept = 0), linetype = 2) + 
  coord_flip(ylim = c(-2, 2)) + 
  geom_linerange(aes(ymin = ci_lower_95, ymax = ci_upper_95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  theme_minimal(base_size = 16) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = NA, color = "black")) +
  scale_color_manual(values = palette_main) +
  ylab(" ") + 
  xlab("") +
  facet_grid(outcome_type ~., scale = 'free') 
hessen2013

## Placebo using state elections in other states (spillover effects?)
spillover <- data %>%
  filter(Sample == "Placebo with other election dates") %>%
  mutate(Outcome = fct_reorder(Outcome, coef), desc = T) %>%
  mutate(Outcome = fct_reorder(Outcome, pca), desc = F) %>%  
  ggplot(aes(Outcome, coef, color = factor(pca))) + 
  geom_hline(aes(yintercept = 0), linetype = 2) + 
  coord_flip(ylim = c(-2, 2)) + 
  geom_linerange(aes(ymin = ci_lower_95, ymax = ci_upper_95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  theme_minimal(base_size = 16) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = NA, color = "black")) +
  scale_color_manual(values = palette_main) +
  ylab(" ") + 
  xlab("") +
  facet_grid(outcome_type ~ Model, scale = 'free') 
spillover

ggsave("Drafts/spillover.png", plot = spillover, width = 55, height = 20, units = "cm") 


